//
//  LGCareTemplateInterveneController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/7/30.
//  模版导入 - 介入治疗 、药物治疗

#import "LGCareTemplateImportController.h"
#import "DatePickerView.h"
@interface LGCareTemplateImportController ()<LGBottomButtonDelegate>
/**  手术类型 */
@property (nonatomic , strong)UILabel *surgeryTitle;
@property (nonatomic , strong)UIButton *sugeryBtn1;
@property (nonatomic , strong)UIButton *sugeryBtn2;

/**  出院、用药时间 */
@property (nonatomic , strong)UILabel *timeTitle;
@property (nonatomic , strong)UIButton *timeBtn;

/**  疾病 */
@property (nonatomic , strong)UILabel *diseaseTitle;
@property (nonatomic , strong)UIButton *diseaseBtn1;
@property (nonatomic , strong)UIButton *diseaseBtn2;
@property (nonatomic , strong)UIButton *diseaseBtn3;

/**  体征信息 */
@property (nonatomic , strong)UILabel *bodayTitle;
@property (nonatomic , strong)UILabel *heightTitle;
@property (nonatomic , strong)UITextField *heightTF;
@property (nonatomic , strong)UILabel *weightTitle;
@property (nonatomic , strong)UITextField *weightTF;

/**  确定 */
@property (nonatomic , strong)LGBottomButtonView *bottomView;
/**   */
@property (nonatomic , strong)NSMutableArray *sugeryArray;
/**   */
@property (nonatomic , strong)NSMutableArray *diseaseArray;
/**   */
@property (nonatomic , copy)NSString *currentDate;

@end

@implementation LGCareTemplateImportController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.currentDate = @"";
    self.title = [self.type isEqualToString:@"intervene"]?@"房颤介入治疗":@"房颤药物治疗";
    [self createSubView];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient.Profile/detailBaseFc" parameters:@{@"patient_id":self.patientid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            NSDictionary *patientDic = returnData[@"base"];
            self.heightTF.text = [LGNSString(patientDic[@"height"]) isEmptyString]?@"":LGNSString(patientDic[@"height"]);
            self.weightTF.text = [LGNSString(patientDic[@"weight"]) isEmptyString]?@"":LGNSString(patientDic[@"weight"]);
        }
    } error:^(NSError *error) {
        
    }];
}

//选择手术类型
-(void)selectSugeryAction:(UIButton *)sender{
    sender.selected = !sender.selected;
    NSString *title = (sender == self.sugeryBtn1)?@"房颤导管消融":@"左心耳封堵";
    if (sender.selected == YES) {
        [self.sugeryArray addObject:title];
    }else if([self.sugeryArray containsObject:title]){
        [self.sugeryArray removeObject:title];
    }
    NSString *borderColor = (sender.selected == YES)?@"#5E8EF5":@"#EEEEEE";
    sender.layer.borderColor = [UIColor colorWithString:borderColor].CGColor;
}

//选择时间
-(void)selectTimeAction:(UIButton *)sender{
    DatePickerView *pickerView = [[DatePickerView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    pickerView.currentDate = self.currentDate;
    pickerView.title = @"选择时间";
    [[ObjectTool mainWindow] addSubview:pickerView];
    __weak typeof(self) weakSelf = self;
    pickerView.selectDate = ^(NSString *date) {
        weakSelf.currentDate = date;
        [weakSelf.timeBtn setTitle:date forState:UIControlStateNormal];
    };
}

//选择疾病
-(void)selectDiseaseAction:(UIButton *)sender{
    sender.selected = !sender.selected;
    NSString *title = (sender==self.diseaseBtn1)?@"高血压":((sender==self.diseaseBtn2)?@"糖尿病":@"以上均无");
    if (sender.selected == YES) {
        if (sender == self.diseaseBtn3) {
            //以上均无
            self.diseaseBtn1.selected = NO;
            self.diseaseBtn2.selected = NO;
            self.diseaseBtn1.layer.borderColor = [UIColor colorWithString:@"#EEEEEE"].CGColor;
            self.diseaseBtn2.layer.borderColor = [UIColor colorWithString:@"#EEEEEE"].CGColor;
            [self.diseaseArray removeAllObjects];
            [self.diseaseArray addObject:title];
        }else{
            self.diseaseBtn3.selected = NO;
            self.diseaseBtn3.layer.borderColor = [UIColor colorWithString:@"#EEEEEE"].CGColor;
            if ([self.diseaseArray containsObject:@"以上均无"]) {
                [self.diseaseArray removeObject:@"以上均无"];
            }
            [self.diseaseArray addObject:title];
        }
    }else if([self.diseaseArray containsObject:title]){
        [self.diseaseArray removeObject:title];
    }
    NSString *borderColor = (sender.selected == YES)?@"#5E8EF5":@"#EEEEEE";
    sender.layer.borderColor = [UIColor colorWithString:borderColor].CGColor;
}

-(void)textFieldEditingChanged:(UITextField *)textField{
    textField.text = (textField.text.length>7)?[textField.text substringToIndex:7]:textField.text;
}

-(void)bottomButtonTouched{
    NSString *height = (self.heightTF.text.length>0)?self.heightTF.text:@"";
    NSString *weight = (self.weightTF.text.length>0)?self.weightTF.text:@"";
    if ([self.type isEqualToString:@"intervene"] && self.sugeryArray.count == 0) {
        [TooltipView showMessage:@"请选择手术类型" offset:0];
        return;
    }
    if (self.currentDate.length == 0) {
        [TooltipView showMessage:@"请选择时间" offset:0];
        return;
    }
    if (self.diseaseArray.count == 0) {
        [TooltipView showMessage:@"请选择患者是否患有疾病" offset:0];
        return;
    }
    if (height.length == 0) {
        [TooltipView showMessage:@"请输入身高" offset:0];
        return;
    }
    if (weight.length == 0) {
        [TooltipView showMessage:@"请输入体重" offset:0];
        return;
    }
    [RequestUtil withPOST:@"/doctor/Careplan/saveCarePlanTaskFinish" parameters:@{@"patient_id":self.patientid,@"applytypes":self.sugeryArray,@"careplan_date":self.currentDate,@"jbs":self.diseaseArray,@"height":height,@"weight":weight} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            [TooltipView showMessage:@"保存成功" offset:0];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        
    }];
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    if ([self.type isEqualToString:@"intervene"]) {
        [self.view addSubview:self.surgeryTitle];
        [self.view addSubview:self.sugeryBtn1];
        [self.view addSubview:self.sugeryBtn2];
    }
    [self.view addSubview:self.timeTitle];
    [self.view addSubview:self.timeBtn];
    [self.view addSubview:self.diseaseTitle];
    [self.view addSubview:self.diseaseBtn1];
    [self.view addSubview:self.diseaseBtn2];
    [self.view addSubview:self.diseaseBtn3];
    [self.view addSubview:self.bodayTitle];
    [self.view addSubview:self.heightTitle];
    [self.view addSubview:self.heightTF];
    [self.view addSubview:self.weightTitle];
    [self.view addSubview:self.weightTF];
    [self.view addSubview:self.bottomView];
}

-(NSMutableArray *)sugeryArray{
    if(!_sugeryArray){
        _sugeryArray = [NSMutableArray array];
    }
    return _sugeryArray;
}

-(NSMutableArray *)diseaseArray{
    if(!_diseaseArray){
        _diseaseArray = [NSMutableArray array];
    }
    return _diseaseArray;
}

-(UILabel *)surgeryTitle{
    if(!_surgeryTitle){
        _surgeryTitle = [UILabel lableWithText:@"请选择患者的手术类型(可多选)" colorString:@"#282828" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _surgeryTitle.frame = CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), viewPix(22));
    }
    return _surgeryTitle;
}

-(UIButton *)sugeryBtn1{
    if(!_sugeryBtn1){
        _sugeryBtn1 = [UIButton buttonWithTitle:@"房颤导管消融" titleFont:LGFont(16) normalColor:@"#606266" selectColor:@"#5E8EF5" frame:CGRectMake(viewPix(16), kMaxY(self.surgeryTitle.frame)+viewPix(8), (Screen_W-viewPix(40))/2.0, viewPix(40))];
        [_sugeryBtn1 setBackgroundColor:@"#FEFEFE" selectColor:@"#E5F1FF"];
        [_sugeryBtn1 addBorder:@"#EEEEEE" lineWidth:1 cornerRidus:3];
        [_sugeryBtn1 addTarget:self action:@selector(selectSugeryAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sugeryBtn1;
}

-(UIButton *)sugeryBtn2{
    if(!_sugeryBtn2){
        _sugeryBtn2 = [UIButton buttonWithTitle:@"左心耳封堵" titleFont:LGFont(16) normalColor:@"#606266" selectColor:@"#5E8EF5" frame:CGRectMake(kMaxX(self.sugeryBtn1.frame)+viewPix(8), kMaxY(self.surgeryTitle.frame)+viewPix(8), (Screen_W-viewPix(40))/2.0, viewPix(40))];
        [_sugeryBtn2 setBackgroundColor:@"#FEFEFE" selectColor:@"#E5F1FF"];
        [_sugeryBtn2 addBorder:@"#EEEEEE" lineWidth:1 cornerRidus:3];
        [_sugeryBtn2 addTarget:self action:@selector(selectSugeryAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sugeryBtn2;
}

-(UILabel *)timeTitle{
    if(!_timeTitle){
        NSString *title = [self.type isEqualToString:@"intervene"]?@"请选择患者的出院时间":@"请选择您最近一次调整房颤用药的时间";
        CGFloat top = [self.type isEqualToString:@"intervene"]?viewPix(100):viewPix(16);
        _timeTitle = [UILabel lableWithText:title colorString:@"#282828" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _timeTitle.frame = CGRectMake(viewPix(16), top, Screen_W-viewPix(32), viewPix(22));
    }
    return _timeTitle;
}

-(UIButton *)timeBtn{
    if(!_timeBtn){
        _timeBtn = [UIButton buttonWithBackColor:@"#FEFEFE" cornerRadius:3 title:@"请选择时间" titleFont:LGFont(16) textColor:@"#606266" frame:CGRectMake(viewPix(16), kMaxY(self.timeTitle.frame)+viewPix(8), Screen_W-viewPix(32), viewPix(40))];
        [_timeBtn addBorder:@"#EEEEEE" lineWidth:1 cornerRidus:3];
        [_timeBtn addTarget:self action:@selector(selectTimeAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _timeBtn;
}

-(UILabel *)diseaseTitle{
    if(!_diseaseTitle){
        _diseaseTitle = [UILabel lableWithText:@"患者是否有以下疾病" colorString:@"#282828" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _diseaseTitle.frame = CGRectMake(viewPix(16), kMaxY(self.timeBtn.frame)+viewPix(20), Screen_W-viewPix(32), viewPix(22));
    }
    return _diseaseTitle;
}

-(UIButton *)diseaseBtn1{
    if(!_diseaseBtn1){
        _diseaseBtn1 = [UIButton buttonWithTitle:@"高血压" titleFont:LGFont(16) normalColor:@"#606266" selectColor:@"#5E8EF5" frame:CGRectMake(viewPix(16), kMaxY(self.diseaseTitle.frame)+viewPix(8), (Screen_W-viewPix(48))/3.0, viewPix(40))];
        [_diseaseBtn1 setBackgroundColor:@"#FEFEFE" selectColor:@"#E5F1FF"];
        [_diseaseBtn1 addBorder:@"#EEEEEE" lineWidth:1 cornerRidus:3];
        [_diseaseBtn1 addTarget:self action:@selector(selectDiseaseAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _diseaseBtn1;
}

-(UIButton *)diseaseBtn2{
    if(!_diseaseBtn2){
        _diseaseBtn2 = [UIButton buttonWithTitle:@"糖尿病" titleFont:LGFont(16) normalColor:@"#606266" selectColor:@"#5E8EF5" frame:CGRectMake(kMaxX(self.diseaseBtn1.frame)+viewPix(8), kMinY(self.diseaseBtn1.frame), (Screen_W-viewPix(48))/3.0, viewPix(40))];
        [_diseaseBtn2 setBackgroundColor:@"#FEFEFE" selectColor:@"#E5F1FF"];
        [_diseaseBtn2 addBorder:@"#EEEEEE" lineWidth:1 cornerRidus:3];
        [_diseaseBtn2 addTarget:self action:@selector(selectDiseaseAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _diseaseBtn2;
}

-(UIButton *)diseaseBtn3{
    if(!_diseaseBtn3){
        _diseaseBtn3 = [UIButton buttonWithTitle:@"以上均无" titleFont:LGFont(16) normalColor:@"#606266" selectColor:@"#5E8EF5" frame:CGRectMake(kMaxX(self.diseaseBtn2.frame)+viewPix(8), kMinY(self.diseaseBtn1.frame), (Screen_W-viewPix(48))/3.0, viewPix(40))];
        [_diseaseBtn3 setBackgroundColor:@"#FEFEFE" selectColor:@"#E5F1FF"];
        [_diseaseBtn3 addBorder:@"#EEEEEE" lineWidth:1 cornerRidus:3];
        [_diseaseBtn3 addTarget:self action:@selector(selectDiseaseAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _diseaseBtn3;
}

-(UILabel *)bodayTitle{
    if(!_bodayTitle){
        _bodayTitle = [UILabel lableWithText:@"患者的体征信息" colorString:@"#282828" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _bodayTitle.frame = CGRectMake(viewPix(16), kMaxY(self.diseaseBtn1.frame)+viewPix(20), Screen_W-viewPix(32), viewPix(22));
    }
    return _bodayTitle;
}

-(UILabel *)heightTitle{
    if(!_heightTitle){
        _heightTitle = [UILabel lableWithText:@"身高" colorString:@"#282828" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _heightTitle.frame = CGRectMake(viewPix(16), kMaxY(self.bodayTitle.frame)+viewPix(8), viewPix(90), viewPix(40));
        _heightTitle.attributedText = [@"身高(cm)" attributeString:@{NSFontAttributeName:LGFont(14),NSForegroundColorAttributeName:[UIColor colorWithString:@"#909399"]} range:NSMakeRange(2, 4)];
    }
    return _heightTitle;
}

-(UILabel *)weightTitle{
    if(!_weightTitle){
        _weightTitle = [UILabel lableWithText:@"体重" colorString:@"#282828" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _weightTitle.frame = CGRectMake(viewPix(16), kMaxY(self.heightTitle.frame)+viewPix(16), viewPix(90), viewPix(40));
        _weightTitle.attributedText = [@"体重(kg)" attributeString:@{NSFontAttributeName:LGFont(14),NSForegroundColorAttributeName:[UIColor colorWithString:@"#909399"]} range:NSMakeRange(2, 4)];
    }
    return _weightTitle;
}

-(UITextField *)heightTF{
    if(!_heightTF){
        _heightTF = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(111), kMinY(self.heightTitle.frame), Screen_W-viewPix(127), viewPix(40))];
        _heightTF.backgroundColor = [UIColor colorWithString:@"#F7F9FB"];
        _heightTF.tintColor = [UIColor colorWithString:@"#282828"];
        _heightTF.textColor = [UIColor colorWithString:@"#282828"];
        [_heightTF addBorder:@"#D9D9D9" lineWidth:1 cornerRidus:3];
        _heightTF.textAlignment = NSTextAlignmentLeft;
        _heightTF.keyboardType = UIKeyboardTypeDecimalPad;
        _heightTF.placeholder = @"请输入(必填)";
        _heightTF.font = LGFont(14);
        _heightTF.leftViewMode = UITextFieldViewModeAlways;
        _heightTF.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewPix(12), viewPix(40))];
        [_heightTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _heightTF;
}

-(UITextField *)weightTF{
    if(!_weightTF){
        _weightTF = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(111), kMinY(self.weightTitle.frame), Screen_W-viewPix(127), viewPix(40))];
        _weightTF.backgroundColor = [UIColor colorWithString:@"#F7F9FB"];
        _weightTF.tintColor = [UIColor colorWithString:@"#282828"];
        _weightTF.textColor = [UIColor colorWithString:@"#282828"];
        [_weightTF addBorder:@"#D9D9D9" lineWidth:1 cornerRidus:3];
        _weightTF.textAlignment = NSTextAlignmentLeft;
        _weightTF.keyboardType = UIKeyboardTypeDecimalPad;
        _weightTF.placeholder = @"请输入(必填)";
        _weightTF.font = LGFont(14);
        _weightTF.leftViewMode = UITextFieldViewModeAlways;
        _weightTF.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewPix(12), viewPix(40))];
        [_weightTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _weightTF;
}

-(LGBottomButtonView *)bottomView{
    if(!_bottomView){
        _bottomView = [[LGBottomButtonView alloc]initWithTitle:@"确定" color:@"#5E8EF5" type:LGBottomButtonTypeBack frame:CGRectMake(0, Screen_H-topBarHeight-viewPix(65)-bottomSafeBarHeight/4.0, Screen_W, viewPix(65)+bottomSafeBarHeight/4.0)];
        _bottomView.delegate = self;
    }
    return _bottomView;
}

@end
